﻿using System;
using System.Collections;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Xml.Linq; 
using INTEX.DataBase;
using WPFExcelReport;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class BATCHVKVV:BaseFunction
    {
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public DateTime tomorrow = DateTime.Now.AddDays(1);
        public DateTime yesterday = DateTime.Now.AddDays(-1);
        public string ITXMANDANT = "00", ITXDBVERSION="50.24",ITXUSER = "ITXVVVK ", ITXDATE = "20120723", ITXVKKRS = "C0", ITXVKLFD="100227";

        public string today = "";
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {

            }
            return str;
        }
        OracleCommand or1;
        public OracleConnection OracleDB;
        INTEXDBC INTEXDBC;
        public string ApplicationPath
        {
            get { return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); }
        }
        public BATCHVKVV(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            
            OracleDB = _OracleDB;
            proxy = _proxy;
        }
        /// <summary>
        /// 通过客户0实现不同的组合剪板条件设定。
        /// </summary>
        /// <param name="xdoc"></param>
        public bool Execute(XDocument xdoc = null)
        {
            bool flagValidate = false;
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB,proxy);
            }
            DateTime now = DateTime.Now;
            today = now.Year.ToString() + Right("00" + now.Month.ToString(), 2) + Right("00" + now.Day.ToString(), 2);
            ITXDATE = today;
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow rowVKK;
            DataTable tbVKKOPF = SetData.Tables["VKKOPF"];
            if (tbVKKOPF.Rows.Count == 0)
                return false;
            // KDSTAMM KUNDVER
            /*foreach (DataRow row in SetData.Tables["KDSTAMM"].Rows)
            {
                INTEXDBC.SaveKDSTAMM(row);
            }
            foreach (DataRow row in SetData.Tables["KUNDVER"].Rows)
            {
                INTEXDBC.SaveKUNDVER(row);
            }*/
            rowVKK = tbVKKOPF.Rows[0];
            INTEXDBC.SaveVKKOPF(rowVKK, ITXDBVERSION);
            EventDispatcher.Instance.RaiseStatus("插入销售单:" + rowVKK["VKKAUFKRS"].ToString()+"--"+ rowVKK["VKKAUFLFD"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow row in SetData.Tables["VKPOS"].Rows)
            {
                INTEXDBC.SaveVKPOS(row, ITXDBVERSION);
                EventDispatcher.Instance.RaiseStatus("插入销售单行:" + row["VKPAUFPOS"].ToString(), StatusEventType.RunningProgress);
            }
              //WDECK WBBST WBBEW VVLIEF VVTLIEF
           
            foreach (DataRow row in SetData.Tables["WDECK"].Rows)
            {
                INTEXDBC.SaveWDECK(row, ITXDBVERSION);
                EventDispatcher.Instance.RaiseStatus("插入面料需求:" + row["WDAUFPOS"].ToString(), StatusEventType.RunningProgress);
            }
            foreach (DataRow row in SetData.Tables["WBBST"].Rows)
            {
                INTEXDBC.SaveWBBST(row, ITXDBVERSION);
                EventDispatcher.Instance.RaiseStatus("插入新布卷:" + row["WBBSTKNR"].ToString(), StatusEventType.RunningProgress);
            }
           
            foreach (DataRow row in SetData.Tables["WBBEW"].Rows)
            {
                INTEXDBC.SaveWBBEW(row);
            } 
            foreach (DataRow row in SetData.Tables["VVLIEF"].Rows)
            {
                INTEXDBC.SaveVVLIEF(row, ITXDBVERSION);
                EventDispatcher.Instance.RaiseStatus("插入发货单:" + row["VVLIEFSCH"].ToString(), StatusEventType.RunningProgress);
            }
            foreach (DataRow row in SetData.Tables["VVTLIEF"].Rows)
            {
                INTEXDBC.SaveVVTLIEF(row, ITXDBVERSION);
                EventDispatcher.Instance.RaiseStatus("插入发货单行:" + row["VVTLIEFSCH"].ToString(), StatusEventType.RunningProgress);
            }
            /*foreach (DataRow row in SetData.Tables["WBDAT"].Rows)
            {
                INTEXDBC.SaveWBDAT(row,ITXDBVERSION);
            }*/
            /*
            Sum the new WBBST and update the original one.汇总新布卷信息，或者WBBEW，更新原布卷相关信息，
            ///例如待确认剪布数量
             * 如果WBBEW的WBEWBELEG==0，先插入进去，然后更新ID
             * 如果 WBBEW的WBEWBUCHUHR==0，先插入进去，然后更新ID
             * 如果   VVLIEF .VVLIEFSCH==0，先插入进去，然后更新ID
             */
            //WBBST
            //INTEXDBC.SaveAllocateWB("000000");
            //INTEXDBC.SaveVV("000000");
            return true;
        }
        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXVKKRS = dwIntex["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntex["ITXVKLFD"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete()
        {
            try
            {
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}